package so.ifl.servcie.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.commons.tools.DateUtil;
import so.ifl.commons.tools.StringHelper;
import so.ifl.dao.ExamBookingDetailInfoDao;
import so.ifl.dao.ExamBookingInfoDao;
import so.ifl.dao.ExamHistoryDao;
import so.ifl.dao.ResultInfoDao;
import so.ifl.dao.StudentInfoDao;
import so.ifl.servcie.ExamBookingDetailService;
import so.ifl.vo.ExamBookingDetailVo;
import so.ifl.vo.ExamBookingVo;
import so.ifl.vo.ExamHistoryVo;
import so.ifl.vo.ResultVo;
import so.ifl.vo.StudentVo;
/**
 * 
 * create by simon
 * create at 2012-1-18 上午9:12:46
 * <mailto:liangyuanliu@gmail.com>
 */
@Service("exambookingdetailService")
public class ExamBookingDetailServcieImpl implements ExamBookingDetailService {

	@Autowired
	private ExamBookingDetailInfoDao exambookingdetailInfoDao;
	
	@Autowired
	private ResultInfoDao resultInfoDao;
	
	@Autowired
	private StudentInfoDao studentInfoDao;
	
	@Autowired
	private ExamBookingInfoDao exambookingInfoDao;
	
	@Autowired
	private ExamHistoryDao examHistoryDao;

	public List<ExamBookingDetailVo> queryExamBookingDetailWithParamAndPagination(
			Map<String, Object> params, Integer offset, Integer limit) {
		return exambookingdetailInfoDao.queryExamBookingDetailWithParamAndPagination(
				params, new RowBounds(offset, limit));
	}
	
	public List<ExamBookingDetailVo> queryExamBookingDetailWithParamAndPaginationASC(Map<String, Object> params, Integer offset, Integer limit){
		return exambookingdetailInfoDao.queryExamBookingDetailWithParamAndPaginationASC(params, new RowBounds(offset, limit));
	}

	public Integer countExamBookingDetailWithParam(Map<String, Object> params) {
		return exambookingdetailInfoDao.countExamBookingDetailWithParam(params);
	}

	public void insertExamBookingDetail(ExamBookingDetailVo examBookingDetailVo) {
		exambookingdetailInfoDao.insertExamBookingDetail(examBookingDetailVo);
	}

	public void updateExamBookingDetail(Map<String, Object> params) {
		exambookingdetailInfoDao.updateExamBookingDetail(params);
		Integer student_id_ = (Integer)params.get("student_id_");
		String exam_result_ = (String)params.get("exam_result_");
		Integer exam_booking_id_ = (Integer)params.get("exam_booking_id_");
		if (null != student_id_ && StringHelper.isNotEmpty(exam_result_) && null != exam_booking_id_) {
			Map<String, Object> qparams = new HashMap<String, Object>();
			qparams.put("student_id_", student_id_);
			qparams.put("exam_booking_id_", exam_booking_id_);
			List<ResultVo> resultVos = resultInfoDao.queryResultOfStudent(qparams);
			List<StudentVo> studentVos = studentInfoDao.queryStudentWithParamAndPagination(qparams, new RowBounds(0, 10000));
			List<ExamBookingVo> examBookingVos = exambookingInfoDao.queryExamBookingWithParamAndPagination(qparams);
			if (null != resultVos && resultVos.size() > 0 && null != examBookingVos && examBookingVos.size() > 0 && null != examBookingVos && examBookingVos.size() > 0) {
				ResultVo resultVo = resultVos.get(0);
				StudentVo studentVo = studentVos.get(0);
				ExamBookingVo examBookingVo = examBookingVos.get(0);
				if (null != examBookingVo.getExaminationSubjects()) {
					String source = DateUtil.YMD_SDF.format(new Date());
					switch (examBookingVo.getExaminationSubjects()) {
					case 1:
						resultVo.setTest1Result(exam_result_);
						resultVo.setSource1(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("2");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("3");
						}
						break;
					case 2:
						resultVo.setTest2Result(exam_result_);
						resultVo.setSource2(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("4");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("5");
						}
						break;
					case 3:
						resultVo.setTest3Result(exam_result_);
						resultVo.setSource3(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("6");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("7");
						}
						break;
					case 4:
						resultVo.setTest4Result(exam_result_);
						resultVo.setSource4(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("8");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("8");
						}
						break;
					case 5:
						resultVo.setTest5Result(exam_result_);
						resultVo.setSource5(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("9");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("9");
						}
						break;
					case 6:
						resultVo.setTest6Result(exam_result_);
						resultVo.setSource6(source);
						if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("100")) {
							studentVo.setStudentProgress("11");
						} else if (StringHelper.isNotEmpty(exam_result_) && exam_result_.equals("50")) {
							studentVo.setStudentProgress("10");
						}
						break;

					default:
						break;
					}
				}
				
				String account_login_name_ = (String)params.get("account_login_name_");
				String account_owner_ = (String)params.get("account_owner_");
				ExamHistoryVo examHistoryVo = new ExamHistoryVo(
						student_id_,
						examBookingVo.getExamBookingDate(),
						new Date(),
						examBookingVo.getExaminationSubjects(),
						exam_result_,
						examBookingVo.getExamCode(),
						account_owner_,
						account_login_name_,
						new Date(),
						account_login_name_, 
						new Date());
				
				examHistoryDao.insertExamHistory(examHistoryVo);//仅回表时需要记录考试记录 20130806
				resultInfoDao.updateResult(resultVo);
				studentInfoDao.updateStudent(studentVo);
			}		
		}
		

	}

	public void deleteExamBookingDetail(Integer exambookingdetailId) {
		exambookingdetailInfoDao.deleteExamBookingDetailBySelf(exambookingdetailId);
	}

	
}
